package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CTrucks;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICTrucksService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
public class TruckAdvancedBu {
    @Autowired
    private ICTrucksService icTrucksService;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    UserInfoDao userInfoDao;


    //车辆的高级操作，如：是否识别排队,是否识别装车,登记信息是否批准,是否必须排队,刷卡进门后，是否允许有几分钟的延时时间过磅  0不允许 1允许
    public SzResult TruckAdvanced(String id, Integer identifyQueueStatus, Integer identifyLoadStatus, Integer approveStatus, Integer approveQueueStatus,
                                  Integer delayTimeStatus,String trucksAgainMinTm, String token,String plantId) throws Exception {
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        if (identifyQueueStatus != null) {
            if (identifyQueueStatus == 1) {
                cTrucks.setTrucksIdentyQueue(1);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("必须识别排队", user.getTrueName(), "添加了车牌号为" + trucksTruckno + "的车辆必须识别排队的信息", plantId);
            } else {
                cTrucks.setTrucksIdentyQueue(0);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("取消必须识别排队", user.getTrueName(), "取消了车牌号为" + trucksTruckno + "的车辆必须识别排队的信息", plantId);
            }
        }
        if (identifyLoadStatus != null) {
            if (identifyLoadStatus == 1) {
                cTrucks.setTrucksIdentyLoad(1);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("必须识别装车", user.getTrueName(), "添加了车牌号为" + trucksTruckno + "的车辆必须识别装车的信息", plantId);
            } else {
                cTrucks.setTrucksIdentyLoad(0);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("取消必须识别装车", user.getTrueName(), "取消了车牌号为" + trucksTruckno + "的车辆必须识别装车的信息", plantId);
            }
        }
        if (approveStatus != null) {
            if (approveStatus == 1) {
                cTrucks.setTrucksInfoApproved(1);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("登记信息必须批准", user.getTrueName(), "添加了车牌号为" + trucksTruckno + "的车辆登记信息必须批准的信息", plantId);
            } else {
                cTrucks.setTrucksInfoApproved(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("取消登记信息必须批准", user.getTrueName(), "取消了车牌号为" + trucksTruckno + "的车辆登记信息必须批准的信息", plantId);
            }
        }
        if (approveQueueStatus != null) {
            if (approveQueueStatus == 1) {
                cTrucks.setTrucksMustQueue(1);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("必须排队", user.getTrueName(), "添加了车牌号为" + trucksTruckno + "的车辆必须排队的信息",plantId);
            } else {
                cTrucks.setTrucksMustQueue(0);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("取消必须排队", user.getTrueName(), "取消了车牌号为" + trucksTruckno + "的车辆必须排队的信息",plantId);
            }
        }
        if (delayTimeStatus != null) {
            if (delayTimeStatus == 1) {
                cTrucks.setTrucksEntered(1);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("刷卡进门后允许有几分钟的延时时间过磅", user.getTrueName(), "添加了车牌号为" + trucksTruckno + "的车辆刷卡进门后允许有几分钟的延时时间过磅的信息",plantId);
            } else {
                cTrucks.setTrucksEntered(0);
                cTrucks.setDownedID1(0);
                icTrucksService.updateById(cTrucks);
                String trucksTruckno = cTrucks.getTrucksTruckno();
                sysLogBu.logRecord("取消刷卡进门后允许有几分钟的延时时间过磅", user.getTrueName(), "取消了车牌号为" + trucksTruckno + "的车辆刷卡进门后允许有几分钟的延时时间过磅的信息",plantId);
            }
        }
        if (trucksAgainMinTm != null) {
            cTrucks.setTrucksAgainMinTm(new BigDecimal(trucksAgainMinTm));
            cTrucks.setDownedID1(0);
            icTrucksService.updateById(cTrucks);
            String trucksTruckno = cTrucks.getTrucksTruckno();
           // sysLogBu.logRecord("刷卡进门后允许有几分钟的延时时间过磅", user.getTrueName(), "修改了车牌号为" + trucksTruckno + "的车辆刷卡进门后允许有几分钟的延时时间过磅的信息",plantId);
            sysLogBu.logRecord("设置了车牌号为" + trucksTruckno + "的车辆的排队间隔时间", user.getTrueName(), "设置了车牌号为" + trucksTruckno + "的排队间隔时间为"+trucksAgainMinTm,plantId);
        }

            return SzResult.Success();
    }
}
